/*
 * @Author: liyin 2179253556@qq.com
 * @Date: 2023-03-07 16:16:18
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2025-04-15 09:09:29
 * @FilePath: index.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { createStore, createLogger } from "vuex";
import { getFacullyApi, getGradeApi, getClassApi } from '../components/Api/IndexApi'
import { Toast } from 'vant'
export default createStore({
    state: {
        loginForm: window.sessionStorage.getItem('loginForm') ? JSON.parse(window.sessionStorage.getItem('loginForm') as any) : 
        // {},
        {
            accessToken: "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJyb2xlX25hbWUiOiJcdThkODVcdTdlYTciLCJpZCI6IjEwMDI4NDMzIiwicm9sZXR5cGUiOiIxNiIsInZpc2l0b3JzX3R5cGUiOjEzLCJuYW1lIjoiXHU2NzRlKiIsImluY2giOiIvU2Nob29sVXNlci9QdWJsaWMvaW5jaC84NGZoQVF5QjYzZEIzajJ0d2RHbWJHS3BIeVRaS2J4RS5qcGciLCJnZW5kZXIiOiIxIiwicGhvbmUiOiIxOTEqKioqNTUzNSIsInJvbGVfdHlwZSI6MSwic2Nob29sX21hbmFnZXJpZCI6IjEwMDI1MjE0IiwiaXNfYWRtaW4iOnRydWUsImxvZ2luX3R5cGUiOjIsInNjaG9vbF9pZCI6IjczIiwib3BlbmlkIjpudWxsLCJzdHVkZW50X251bSI6IjIifQ.0Rq9Q8pxodRofHlFFfX_ne7JFw4bmEe2rvbZVAAauyw",
            gender: "1",
            id: "41",
            inch: "/SchoolUser/Public/inch/84fhAQyB63dB3j2twdGmbGKpHyTZKbxE.jpg",
            is_admin: true,
            login_type: 2,
            money: "0.00",
            name: "水电费第三方是多少",
            openid: null,
            phone: '191****5567',
            role_name: "家长",
            role_type: 1,
            roletype: "16",
            school_id: "73",
            school_logo: "/SchoolUser/Public/upload/SWbwjf6QmR3mh5EAamp4S46THS5Db8hM.png",
            school_managerid: "10025214",
            school_name: "智慧校园演示学校",
            student_num: "2",
            visitors_type: 13,
        },
        // 记录index.vue下的tabs标签
        active: '1',
        student_info: [{
            bed: "02",
            campusid: "2107652695",
            class_name: "初中部-2021级-06班",
            classid: "4021",
            faculty_id: "77",
            floor: "至美楼",
            floor_level: "2F",
            gender: "2",
            grade_id: "351",
            id: "80118675",
            inch: "/SchoolUser/Public/inch/Yznn4Eb25TNfPiMpXk3AMwZShaYwKbRx.jpeg",
            money: "77.00",
            name: "刘可馨",
            room: "201"
        }],
        // 班主任或者生活老师选中的班级信息
        class: {
            class_id: '',
            class_name: ''
        },
        facully: [],
        grade: [],
        classl: [],
        // 班主任或者生活老师所管班级
        classList: []
    },
    mutations: {
        SET_LOGINFORM(store, val) {
            store.loginForm = val
        },
        SET_ACTIVE(store, val) {
            store.active = val
        },
        SET_CLASS(store, val) {
            store.class = val
        },
        SET_CLASSL(store, val) {
            store.classl = val
        },
        SET_CLASS_LIST(store, val) {
            store.classList = val
        },
        SET_FACULLY(store, val) {
            store.facully = val
        },
        SET_GRADE(store, val) {
            store.grade = val
        },
    },
    actions: {
        setLoginForm(context, obj) {
            context.commit('SET_LOGINFORM', obj)
        },
        setActive(context, obj) {
            context.commit('SET_ACTIVE', obj)
        },
        setClass(context, obj) {
            context.commit('SET_CLASS', obj)
        },
        setClassList(context, obj) {
            context.commit('SET_CLASS_LIST', obj)
        },
        // 获取学部
        async setFacully(context, obj) {
            const res: any = await getFacullyApi()
            if (res.code !== 200) {
                return Toast(res.msg)
            }
            const str: any = []
            if (res.data && res.data.length > 0) {
                res.data.forEach((item: any) => {
                    if (Number(item.facultyStatus) === 1) {
                        const params = {
                            id: item.id,
                            name: item.facultyName,
                            children: []
                        }
                        str.push(params)
                    }
                })
            }
            context.commit('SET_FACULLY', str)
        },
        // 获取年级
        async setGrade(context, obj) {
            let res: any = null
            if (obj.facully_id) {
                res = await getGradeApi({ facultyId: obj.facully_id })
            }
            if (res.code !== 200) {
                context.commit('SET_GRADE', [])
                return Toast(res.msg)
            }
            const str: any = []
            if (res.data && res.data.length > 0) {
                res.data.forEach((item: any) => {
                    const params = {
                        id: item.id,
                        name: item.gradeName,
                        children: []
                    }
                    str.push(params)
                })
            }
            context.commit('SET_GRADE', str)
            return str
        },
        async setGrade2(context, obj) {
            let res: any = null
            if (obj.facully_id) {
                res = await getGradeApi({ facultyId: obj.facully_id })
            }
            if (res.code !== 200) {
                context.commit('SET_GRADE', [])
                return Toast(res.msg)
            }
            const str: any = []
            if (res.data && res.data.length > 0) {
                res.data.forEach((item: any) => {
                    const params = {
                        id: item.id,
                        name: item.gradeName,
                        // children: []
                    }
                    str.push(params)
                })
            }
            context.commit('SET_GRADE', str)
            return str
        },
        // 获取班级
        // 获取班级1
        async setClassDate(context, obj) {
            let res: any = null
            if (obj.grade_id) {
                res = await getClassApi({ gradeId: obj.grade_id })
            }
            if (res.code !== 200) {
                return Toast(res.msg)
            }
            const str: any = []
            if (res.data && res.data.length > 0) {
                res.data.forEach((item: any) => {
                    if (item.classStatus && Number(item.classStatus) === 1) {
                        let params = {}
                        params = {
                            id: item.id,
                            name: item.className,
                        }
                        str.push(params)
                    }
                })
            }
            context.commit('SET_CLASSL', str)
            return str
        }
    },
    plugins: import.meta.env.DEV ? [createLogger()] : []
});